/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aburakc;

import aburakc.deliver.SMSDeliver;
import aburakc.message.SMSTextMessage;
import java.io.IOException;
import java.io.InterruptedIOException;
import javax.microedition.io.Connector;
import javax.microedition.io.PushRegistry;
import javax.wireless.messaging.BinaryMessage;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;

/**
 *
 * @author burak.costu
 */
public class SMSReceiver implements Runnable, MessageListener {

    private Thread thread;
    private boolean done = false;
    private Message message;
    private MainMIDlet mainMIDlet;
    private static SMSReceiver instance;
    private SMSReceiver(){
        
    }

    public static void createInstance(MainMIDlet mIDlet){
        instance = new SMSReceiver();
        instance.setMainMIDlet(mIDlet);
    }

    public static SMSReceiver getInstance(){
        return instance;
    }

    public void run() {
        try {
            message = getMainMIDlet().getMessageConnection().receive();

            if (message != null) {
                getMainMIDlet().addScreenMessage("Mesaj Alınd : " + message.getAddress());
                SMSTextMessage smsMessage = new SMSTextMessage();
                smsMessage.setAddress(message.getAddress());
                smsMessage.setDate(message.getTimestamp());
                long id = System.currentTimeMillis();
                smsMessage.setMessageId(id);
                
                if (message instanceof TextMessage) {
                    smsMessage.setMessage(((TextMessage) message).getPayloadText());
                } else {
                    StringBuffer buf = new StringBuffer();
                    byte[] data = ((BinaryMessage) message).getPayloadData();

                    for (int i = 0; i < data.length; i++) {
                        int intData = (int) data[i] & 0xFF;

                        if (intData < 0x10) {
                            buf.append("0");
                        }

                        buf.append(Integer.toHexString(intData));
                        buf.append(' ');
                    }

                    smsMessage.setMessage(buf.toString());
                    
                }
                SMSDeliver.getInstance().addMesssage(smsMessage);
            }
        } catch (IOException ex) {
            //ex.printStackTrace();
        }
        thread = null;
    }

    public void destroy(){
        done = true;
        thread = null;

    }

    public void notifyIncomingMessage(MessageConnection arg0) {
        if (thread == null) {
            done = false;
            thread = new Thread(this);
            thread.start();
            /*try {
                message = messageConnection.receive();

                if (message != null) {
                    SMSTextMessage smsMessage = new SMSTextMessage();
                    smsMessage.setAddress(message.getAddress());
                    smsMessage.setDate(message.getTimestamp());

                    if (message instanceof TextMessage) {
                        smsMessage.setMessage(((TextMessage) message).getPayloadText());
                    } else {
                        StringBuffer buf = new StringBuffer();
                        byte[] data = ((BinaryMessage) message).getPayloadData();

                        for (int i = 0; i < data.length; i++) {
                            int intData = (int) data[i] & 0xFF;

                            if (intData < 0x10) {
                                buf.append("0");
                            }

                            buf.append(Integer.toHexString(intData));
                            buf.append(' ');
                        }

                        smsMessage.setMessage(buf.toString());
                    }
                    delivery.addMesssage(smsMessage);
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }*/
        }
    }


    /**
     * @return the mainMIDlet
     */
    public MainMIDlet getMainMIDlet() {
        return mainMIDlet;
    }

    /**
     * @param mainMIDlet the mainMIDlet to set
     */
    public void setMainMIDlet(MainMIDlet mainMIDlet) {
        this.mainMIDlet = mainMIDlet;
    }
}
